﻿Public Class FileSharingTabControl
    Public Sub Init()
        BrowseBuddies1.Init()
        SharedFolders1.Init()
        TransfersControl1.Init()
        lblScanInfo.Text = String.Empty
        SetupEvents(True)
    End Sub

    Public Sub [Stop]()
        TransfersControl1.Stop()
        BrowseBuddies1.Stop()
        SharedFolders1.Stop()
        SetupEvents(False)
    End Sub

    Private Sub SetupEvents(ByVal startListening As Boolean)
        If startListening Then
            AddHandler FileShare.Engine.Manager.HashProgress, AddressOf EventHandler_HashProgress
            AddHandler FileShare.Engine.Manager.HashFinished, AddressOf EventHandler_HashFinished
            AddHandler FileShare.Engine.Manager.ScanShareStarting, AddressOf EventHandler_ScanShareStarting
            AddHandler FileShare.Engine.Manager.ScanShareProgress, AddressOf EventHandler_ScanShareProgress
            AddHandler FileShare.Engine.Manager.ScanShareCompleted, AddressOf EventHandler_ScanShareCompleted
            AddHandler FileShare.Engine.Manager.PartUploaded, AddressOf EventHandler_PartUploaded
        Else
            RemoveHandler FileShare.Engine.Manager.HashProgress, AddressOf EventHandler_HashProgress
            RemoveHandler FileShare.Engine.Manager.HashFinished, AddressOf EventHandler_HashFinished
            RemoveHandler FileShare.Engine.Manager.ScanShareStarting, AddressOf EventHandler_ScanShareStarting
            RemoveHandler FileShare.Engine.Manager.ScanShareProgress, AddressOf EventHandler_ScanShareProgress
            RemoveHandler FileShare.Engine.Manager.ScanShareCompleted, AddressOf EventHandler_ScanShareCompleted
            RemoveHandler FileShare.Engine.Manager.PartUploaded, AddressOf EventHandler_PartUploaded
        End If
    End Sub

    Private Sub EventHandler_HashProgress(ByVal filePath As String, ByVal percent As Integer, ByVal bytesPerSecond As Integer)
        lblHashStatus.Text = "Hashing file " & filePath
        ProgressBar1.Value = percent
        lblHashSpeed.Text = Framework.Misc.ToReadableSpeed(bytesPerSecond)
    End Sub
    Private Sub EventHandler_HashFinished(ByVal filePath As String)
        lblHashStatus.Text = "Idle"
        ProgressBar1.Value = ProgressBar1.Minimum
        lblHashSpeed.Text = "-"
    End Sub
    Private Sub EventHandler_ScanShareStarting(ByVal share As Entities.Share)
        lblScanStatus.Text = "Scanning share " & share.Name
    End Sub
    Private Sub EventHandler_ScanShareCompleted(ByVal share As Entities.Share)
        lblScanStatus.Text = "Idle"
        lblScanInfo.Text = String.Empty
    End Sub
    Private Sub EventHandler_ScanShareProgress(ByVal share As Entities.Share, ByVal info As String)
        lblScanInfo.Text = info
    End Sub

    Private Sub EventHandler_PartUploaded(ByVal node As Framework.Net.Node, ByVal filePath As String, ByVal partId As Integer)
        Try
            lvwUploadLog.BeginUpdate()
            With lvwUploadLog.Items.Insert(0, Now.ToString("HH:mm:ss.f"))
                .SubItems.Add(node.Buddy.NameLocal)
                .SubItems.Add(partId.ToString)
                .SubItems.Add(IO.Path.GetFileName(filePath))
                .SubItems.Add(IO.Path.GetDirectoryName(filePath))
            End With
            While lvwUploadLog.Items.Count > 500
                lvwUploadLog.Items.RemoveAt(lvwUploadLog.Items.Count - 1)
            End While
            lvwUploadLog.EndUpdate()
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub


End Class
